package cn.five.stream.surfaceEncoder.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import cn.five.stream.surfaceEncoder.filter.FilterManager;

import com.jemi.androidh264streamdemo.R;

public class FilterTask extends AsyncTask<Void, Void, Bitmap>{

	 private Context mContext;
     private FilterManager.FilterType mFilterType;
     private ImageRenderer mRenderer;
     private Bitmap bitmap;
     private ImageInterface imageInterface; 

     public FilterTask(Context context, ImageRenderer renderer,
             FilterManager.FilterType filterType,Bitmap bitmap,ImageInterface imageInterface) {
         mFilterType = filterType;
         mContext = context;
         mRenderer = renderer;
         this.bitmap=bitmap ;
        this.imageInterface=imageInterface;
     }

     @Override protected Bitmap doInBackground(Void... params) {
         final BitmapFactory.Options options = new BitmapFactory.Options();
         options.inScaled = false;
//         final Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.raw_image, options);
         ImageEglSurface imageEglSurface = new ImageEglSurface(bitmap.getWidth(), bitmap.getHeight()); //设置输出宽高,
         imageEglSurface.setRenderer(mRenderer);
         mRenderer.changeFilter(mFilterType);
         mRenderer.setImageBitmap(bitmap);
         imageEglSurface.drawFrame();
         Bitmap filterBitmap = imageEglSurface.getBitmap();
         imageEglSurface.release();
         mRenderer.destroy();

         return filterBitmap;
     }

     @Override protected void onPostExecute(Bitmap bitmap) {
         super.onPostExecute(bitmap);

         if (bitmap == null) {
             return;
         }

         imageInterface.resultBitmap(bitmap);
     }

}
